import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { IsNull, Repository, TreeRepository } from "typeorm";
import { RoleEntity } from "../Entity/role.entity";
import { MenuEntity } from "../Entity/menu.entity";

@Injectable()
export class MenuService {

    constructor(
        @InjectRepository(MenuEntity)
        private readonly menuResposity: Repository<MenuEntity>,
        // @InjectRepository(MenuEntity)
        // private readonly treeResposity: Repository<MenuEntity>,
    ) { }

    async create(menu): Promise<RoleEntity[]> {
        const u = await this.menuResposity.findOne({ where: { menuName: menu.menuName } });

        if (u) {
            throw new HttpException({
                message: 'Input data validation failed',
                error: 'menuName must be unique',
            },
                HttpStatus.BAD_REQUEST);
        }
        return await this.menuResposity.save(menu);

    }

    async findOne(menuId: number): Promise<MenuEntity> {
        let menu = await this.menuResposity.findOne({ where: { id: menuId } });

        let menuEntity = await this.menuResposity.find({ where: { isDeleted: true } });
        let temp = this.allMenu(menuEntity, menu.pid);
        const item = temp.find(item => item.id === menuId);
        // console.log(item);
        if (item) {
            return item;
        } else {
            throw new HttpException({
                message: 'Please  again input menuId validation',
                error: 'This menu is not  exist',
            },
                HttpStatus.BAD_REQUEST);
        }

    }

    async findAll(): Promise<MenuEntity[]> {
        let menuEntity = await this.menuResposity.find({ where: { isDeleted: true } });
        // console.log(menuEntity);

        return this.allMenu(menuEntity, 0);
    }

    allMenu(menu: MenuEntity[], pid: number) {
        const temp = [];
        for (const item of menu) {
            if (item.pid == pid) {
                item.children = this.allMenu(menu, item.id);
                temp.push(item);
            }
        }
        return temp;
    }

    async remove(menuId: number): Promise<void> {
        let menu = await this.menuResposity.findOne({ where: { id: menuId } });

        let menuEntity = await this.menuResposity.find({ where: { isDeleted: true } });
        let temp = this.allMenu(menuEntity, menu.pid);
        const item = temp.find(item => item.id === menuId);
        let menuTemp = this.traver(item);
        console.log(menuTemp);
        menuTemp.forEach(element => {
            this.menuResposity.update({ id: element }, { isDeleted: false });
        })

        // console.log(item);

        // let menEntity = await this.menuResposity.findOne({ where: { id: menuId } });
        // menEntity.isDeleted = false;
        // return menuTemp;
    }

    traver(item: MenuEntity) {
        const temp = [];
        temp.push(item.id);
        if (item.children.length != 0) {
            item.children.forEach(element => {
                temp.push(element.id);
                this.traver(element);
            });
        }
        return temp;
    }

    async update(menu: MenuEntity): Promise<void> {
        await this.menuResposity.update(menu.id, menu);
    }

}